      SUBROUTINE SAGIM2(ITEST,ZML,ETA,NALP)
C
      IMPLICIT REAL*8(A-H,O-Z)
C
      COMMON/CSAGIM/ AZIN(3,3),AZAX(3),AZCG(3),AZMS,AZYY(3,3),AZIAX(3,3)
     2              ,ZZAZ(3,3)
C
      COMMON/DEBUG2/ IOUT,JOUT,KLUGE
C
      COMMON/SAGMWK/ DELT,GAMGM(6),GMRHS,DI,DIGAM(6)
     1              ,DZML(6,6),YAZB(3)
C
      COMMON/SAGOUT/ AZ,AZD,B(3,3),B0(3,3),B0B(3,3),C(3,3),B0BC(3,3)
     1              ,YAZ(3),ZAZM(3)
C
      COMMON/HSAGIM/ HGMB(3)
C
      COMMON/ISAGIM/ IGMBL,NAZIM,NA1
C
      COMMON/ISAGRS/ ISRAST,IRSDUM(6)
C
      COMMON/RPOOL1/ DUMV1(39),OMEG(3),DUMV2(38),YBCM(3),DUMV3(42)
C
      COMMON/RPOOL3/ ZMS,YIZM(3,2)
C
      COMMON/SARSOT/ AZDD,SARHST(3),SARHSR(3)
C
      COMMON/TRQOUT/ OUTTRQ(150)
C
      COMMON/VARBLS/ DEP(150),DER(150)
C
C
      DIMENSION ZML(7,7),ETA(7),OMB(3),OMC(3)
      DIMENSION DM1(3,3),DM2(3,3),DM3(3,3),DM4(3,3)
      DIMENSION V1(3),V2(3),V3(3),V4(3),V5(3),V6(3)
      DIMENSION RHST(3),RHSR(3)
      DIMENSION DRHST(3),DRHSR(3)
      DIMENSION ZELBM(3)
      DIMENSION OMCPG(3)
      DIMENSION ZELC(3)
C
C
      IF(IGMBL.EQ.0) RETURN
C
      IF(ITEST.EQ.2) GO TO 100
C
C     SYSTEM TRANSLATION EQUATION
C
      AZD2=AZD*AZD
      V1(1)=-AZD2*ZAZM(1)
      V1(2)=-AZD2*ZAZM(2)
      V1(3)=0.0D0
      CALL MATV(1,B0B,V1,RHST)
C
C
      V1(1)=-AZD*ZAZM(2)
      V1(2)=AZD*ZAZM(1)
      V1(3)=0.0D0
      CALL MATV(1,B0B,V1,V2)
      RHST(1)=RHST(1)+2.0D0*(OMEG(2)*V2(3)-OMEG(3)*V2(2))
      RHST(2)=RHST(2)+2.0D0*(OMEG(3)*V2(1)-OMEG(1)*V2(3))
      RHST(3)=RHST(3)+2.0D0*(OMEG(1)*V2(2)-OMEG(2)*V2(1))
C
C     SYSTEM ROTATION EQUATION
C
      V1(1)=ZZAZ(2,3)+ZAZM(2)*YAZB(3)
      V1(2)=-ZZAZ(1,3)-ZAZM(1)*YAZB(3)
      V1(3)=ZAZM(1)*YAZB(2)-ZAZM(2)*YAZB(1)
      CALL MATV(1,B0B,V1,V2)
      TWOAZD=2.0D0*AZD
      RHSR(1)=AZD2*V2(1)
      RHSR(2)=AZD2*V2(2)
      RHSR(3)=AZD2*V2(3)
C
C
      CALL MATV(2,B0B,OMEG,OMB)
C
C
      V1(1)=OMB(1)*(ZZAZ(1,2)+ZAZM(1)*YAZB(2))
     1     +OMB(2)*(ZZAZ(2,2)+ZAZM(2)*YAZB(2))
     2     +OMB(3)*(ZZAZ(2,3)+ZAZM(2)*YAZB(3))
C
      V1(2)=-OMB(1)*(ZZAZ(1,1)+ZAZM(1)*YAZB(1))
     1      -OMB(2)*(ZZAZ(2,1)+ZAZM(2)*YAZB(1))
     2      -OMB(3)*(ZZAZ(1,3)+ZAZM(1)*YAZB(3))
C
      V1(3)=OMB(3)*(ZAZM(1)*YAZB(2)-ZAZM(2)*YAZB(1))
C
      CALL MATV(1,B0B,V1,V2)
C
      RHSR(1)=RHSR(1)+TWOAZD*V2(1)
      RHSR(2)=RHSR(2)+TWOAZD*V2(2)
      RHSR(3)=RHSR(3)+TWOAZD*V2(3)
C
      IF(ISRAST.EQ.0) GO TO 2
C
      DO 1 I=1,3
      RHST(I)=RHST(I) - SARHST(I)
      RHSR(I)=RHSR(I) - SARHSR(I)
      DRHSR(I)=0.0D0
      I3=I+3
      ETA(I)=ETA(I) - RHST(I)
      ETA(I3)=ETA(I3)-RHSR(I)
    1 CONTINUE
C
      GO TO 35
C
    2 CONTINUE
C
C     RELATIVE ROTATION EQUATION - AZIMUTH -
C
      RHAZ=-OMB(1)*OMB(1)*(ZZAZ(1,2)+ZAZM(1)*YAZB(2))
     1     +OMB(1)*OMB(2)*(ZZAZ(1,1)+ZAZM(1)*YAZB(1)
     2                    -ZZAZ(2,2)-ZAZM(2)*YAZB(2))
     3     -OMB(1)*OMB(3)*(ZZAZ(2,3)+ZAZM(2)*YAZB(3))
     4     +OMB(2)*OMB(2)*(ZZAZ(2,1)+ZAZM(2)*YAZB(1))
     5     +OMB(2)*OMB(3)*(ZZAZ(1,3)+ZAZM(1)*YAZB(3))
     6     +OMB(3)*OMB(3)*(ZAZM(2)*YAZB(1)-ZAZM(1)*YAZB(2))
C
C
C
      CALL SAGINF(AZIF)
C
      CALL SAGCNT(AZCNT)
C
      OUTTRQ(34)=AZCNT
C
      GMRHS   =-RHAZ-AZIF+AZCNT
C
      DIR   =DI     *GMRHS
C
      DO 10 I=1,3
      I3=I+3
      DRHST(I)=GAMGM(  I)*DIR
      DRHSR(I)=GAMGM(  I3)*DIR
      ETA(I)=ETA(I)-RHST(I)-DRHST(I)
      ETA(I3)=ETA(I3)-RHSR(I)-DRHSR(I)
   10 CONTINUE
C
      DO 20 I=1,6
      DO 20 J=1,6
      ZML(I,J)=ZML(I,J)-DZML(I,J)
   20 CONTINUE
C
      IF(IOUT.EQ.1) GO TO 30
      WRITE(6,1000)
 1000 FORMAT('0',10X,'DEBUG OUTPUT FROM GIMBL2')
      WRITE(6,1001) DELT,DI
 1001 FORMAT('0',1P12E11.4)
      WRITE(6,1001) GAMGM
      WRITE(6,1001) DIGAM
      WRITE(6,1001) RHAZ,AZIF,AZCNT,RHST,RHSR
      WRITE(6,1001) DRHST,DRHSR
      WRITE(6,1002) ZML
      WRITE(6,1003) ETA
 1002 FORMAT('0',2X,'ZML',6X,1P7E13.5)
 1003 FORMAT('0',2X,'ETA',6X,1P7E13.5)
   30 CONTINUE
C
   35 CONTINUE
C
      DO 40 I=1,3
      I1=23+I
      OUTTRQ(I1)=RHSR(I)+DRHSR(I)
   40 CONTINUE
C
C
      RETURN
C
C
  100 CONTINUE
C
C
      IF(ISRAST.NE.0) GO TO 112
C
C     CONSTRUCT DERIVATIVES FOR AZIMUTH AND ELEVATION
C
      DAZD=0.0D0
      DO 110 I=1,6
      DAZD=DAZD-DIGAM(  I)*ETA(I)
  110 CONTINUE
C
C
      DER(NAZIM)=DEP(NA1)
      DER(NA1)=DIR   +DAZD
C
  112 CONTINUE
C
C     CALCULATE MOMENTUM FOR OUTPUT
C
      DO 115 I=1,3
      V1(I)=YBCM(I)/ZMS
  115 CONTINUE
      CALL MATV(2,B0B,V1,V2)
C
      V4(1)=AZD*(-ZZAZ(3,1)-(YAZB(3)-V2(3))*ZAZM(1))
      V4(2)=AZD*(-ZZAZ(3,2)-(YAZB(3)-V2(3))*ZAZM(2))
      V4(3)=AZD*(ZZAZ(1,1)+ZZAZ(2,2)+(YAZB(1)-V2(1))*ZAZM(1)
     1                              +(YAZB(2)-V2(2))*ZAZM(2))
C
      CALL MATV(1,B0B,V4,V1)
C
      DO 120 I=1,3
      HGMB(I)=V1(I)
  120 CONTINUE
C
C
C
      RETURN
C
      END
